﻿using System;
using System.Configuration;
using OpenQA.Selenium;

namespace AutomatedAcceptanceTests.Infrastructure
{
    public class Browser
    {
        private static Browser _instance;
        private static IWebDriver _webDriver;

        private static readonly TimeSpan ElementTimeOut = TimeSpan.FromSeconds(Double.Parse(ConfigurationManager.AppSettings.Get("webElementTimeout")));
        private static readonly TimeSpan ScriptTimeout = TimeSpan.FromSeconds(Double.Parse(ConfigurationManager.AppSettings.Get("scriptTimeout")));
        private static readonly TimeSpan PageLoadTimeout = TimeSpan.FromSeconds(Double.Parse(ConfigurationManager.AppSettings.Get("pageLoadTimeout")));

        public static Browser BrowserInstance
        {
            get { return _instance ?? (_instance = new Browser()); }
        }
        
        public IWebDriver WebDriver
        {
            get
            {
                if (_webDriver == null)
                {
                    _webDriver = WebDriverFactory.GetWebDriver();
                    ApplyWaitTimes();
                }
                return _webDriver;
            }
            set { _webDriver = value; }
        }

        
        private static void ApplyWaitTimes()
        {
            _webDriver.Manage().Timeouts().ImplicitlyWait(ElementTimeOut);
            _webDriver.Manage().Timeouts().SetPageLoadTimeout(ScriptTimeout);
            _webDriver.Manage().Timeouts().SetPageLoadTimeout(PageLoadTimeout);
        }

    }
}